<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="85%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <el-row>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                            <FormItem
                                :label="t('xmwms.relocation.relocation_no')"
                                type="string"
                                v-model="baTable.form.items!.relocation_no"
                                prop="relocation_no"
                                :input-attr="{ disabled: true }"
                                :placeholder="t('xmwms.relocation.automatic system generation')"
                            />
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                            <FormItem
                                :label="t('xmwms.relocation.order_sn')"
                                type="string"
                                v-model="baTable.form.items!.order_sn"
                                prop="order_sn"
                                :placeholder="t('Please input field', { field: t('xmwms.relocation.order_sn') })"
                            />
                        </el-col>
                    </el-row>
                    <FormItem
                        :label="t('xmwms.relocation.remark')"
                        type="textarea"
                        v-model="baTable.form.items!.remark"
                        prop="remark"
                        :input-attr="{ rows: 3 }"
                        @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('xmwms.relocation.remark') })"
                    />

                    <el-card :header="t('xmwms.relocation.product_material_detail')" style="margin: 0 0 10px 60px; font-weight: bold">
                        <el-row :gutter="8">
                            <el-col :xs="24" :sm="5" :md="5" :lg="5" :xl="5" class="ba-array req">
                                {{ t('xmwms.relocation.product_material_name') }}
                            </el-col>
                            <el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="ba-array hide">
                                {{ t('xmwms.relocation.order_detail_coding') }}
                            </el-col>
                            <el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="ba-array req">{{ t('xmwms.relocation.plan_quantity') }}</el-col>
                            <el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="ba-array req">{{ t('xmwms.relocation.actual_quantity') }}</el-col>
                            <el-col :xs="24" :sm="5" :md="5" :lg="5" :xl="5" class="ba-array req">
                                {{ t('xmwms.relocation.source_warehouse_shelf_id') }}
                            </el-col>
                            <el-col :xs="24" :sm="5" :md="5" :lg="5" :xl="5" class="ba-array req">{{
                                t('xmwms.relocation.target_warehouse_shelf_id')
                            }}</el-col>
                            <el-col :xs="24" :sm="3" :md="3" :lg="3" :xl="3" class="ba-array">{{ t('xmwms.relocation.remark') }}</el-col>
                        </el-row>
                        <el-row class="ba-array-item" v-for="(item, idx) in baTable.form.items!.product_detail" :key="idx" :gutter="8">
                            <el-col :xs="24" :sm="5" :md="5" :lg="5" :xl="5">
                                <BaInput
                                    type="remoteSelect"
                                    v-model="item.product_id"
                                    :attr="{
                                        multiple: false,
                                        pk: 'product.id',
                                        field: 'name',
                                        'remote-url': '/admin/xmwms.Product/index?type=relocation&order_product_no=' + item.order_product_no,
                                    }"
                                    placeholder="请选择产品物料"
                                />
                            </el-col>
                            <el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2" class="hide">
                                <BaInput type="string" v-model="item.order_product_no" />
                            </el-col>
                            <el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2">
                                <BaInput type="number" v-model="item.plan_quantity" />
                            </el-col>
                            <el-col :xs="24" :sm="2" :md="2" :lg="2" :xl="2">
                                <BaInput type="number" v-model="item.actual_quantity" />
                            </el-col>
                            <el-col :xs="24" :sm="5" :md="5" :lg="5" :xl="5">
                                <BaInput
                                    type="remoteSelect"
                                    v-model="item.source_warehouse_shelf_id"
                                    :attr="{
                                        multiple: false,
                                        params: { isTree: true },
                                        pk: 'warehouse.id',
                                        field: 'name',
                                        'remote-url': '/admin/xmwms.Warehouse/index?type=source&order_product_no=' + item.order_product_no,
                                    }"
                                    placeholder="请选择源货架"
                                />
                            </el-col>
                            <el-col :xs="24" :sm="5" :md="5" :lg="5" :xl="5">
                                <BaInput
                                    type="remoteSelect"
                                    v-model="item.target_warehouse_shelf_id"
                                    :attr="{
                                        multiple: false,
                                        params: { isTree: true },
                                        pk: 'warehouse.id',
                                        field: 'name',
                                        'remote-url': '/admin/xmwms.Warehouse/index?type=target&order_product_no=' + item.order_product_no,
                                    }"
                                    placeholder="请选择目标货架"
                                />
                            </el-col>
                            <el-col :xs="24" :sm="3" :md="3" :lg="3" :xl="3">
                                <BaInput type="string" v-model="item.remark" />
                            </el-col>
                            <el-col :span="2">
                                <el-button @click="onDelArrayItem(idx)" size="small" icon="el-icon-Delete" circle />
                            </el-col>
                        </el-row>
                        <el-row :gutter="10">
                            <el-col :span="10" :offset="10">
                                <el-button v-blur class="ba-add-array-item" @click="onAddArrayItem" icon="el-icon-Plus">增加一行</el-button>
                            </el-col>
                        </el-row>
                    </el-card>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject, onMounted, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import BaInput from '/@/components/baInput/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'
import { uuid } from '/@/utils/random'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

onMounted(() => {})

const onAddArrayItem = () => {
    baTable.form.items!.product_detail.push({
        product_id: 0,
        order_product_no: uuid(),
        plan_quantity: 1,
        actual_quantity: 0,
        source_warehouse_shelf_id: 0,
        target_warehouse_shelf_id: 0,
        remark: '',
    })
}
const onDelArrayItem = (idx: number) => {
    baTable.form.items!.product_detail.splice(idx, 1)
}
watch(
    () => baTable.form.items!.product_detail,
    (newVal) => {
        baTable.form.items!.product_detail = newVal
    }
)

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    order_sn: [buildValidatorData({ name: 'required', title: t('xmwms.relocation.order_sn') })],
})
</script>

<style scoped lang="scss">
.ba-array {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px 0;
    color: var(--el-text-color-regular);
    font-weight: bold;
}
.ba-array-item {
    margin-bottom: 6px;
}
.ba-add-array-item {
    float: left;
    margin-top: 10px;
}
.hide {
    display: none;
}
</style>
